package com.ccrfid.rmc6.resource.map;

import com.ccrfid.rmc6.appconfig.auth.Secured;
import com.ccrfid.rmc6.entity.asset.AssetView;
import com.ccrfid.rmc6.entity.poi.PoiLocationEntity;
import com.ccrfid.rmc6.entity.video.VideoCameraView;
import com.ccrfid.rmc6.pojo.dto.PersonnelSearchDto;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.resource.auth.pojo.UserPrincipal;
import com.ccrfid.rmc6.service.map.MapService;
import com.ccrfid.rmc6.service.video.VideoCameraService;
import com.ccrfid.rmc6.util.GeomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Secured
@Path("map")
@Produces(MediaType.APPLICATION_JSON)
public class MapResource {
    private static final Logger logger = LoggerFactory.getLogger(MapResource.class);
    @Context
    private SecurityContext securityContext;
    @Autowired
    private MapService mapService;
    @Autowired
    private VideoCameraService videoCameraService;

    @GET
    @Path("search")
    public Map<String, Object> searchAll(@QueryParam("q") String queryStr) {
        if (!StringUtils.isEmpty(queryStr)) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("locationList", mapService.searchLocationInfo(queryStr));
            resultMap.put("personnelList", mapService.searchPersonnelInfo(queryStr));
            return resultMap;
        } else {
            return null;
        }
    }

    @GET
    @Path("search/locations")
    public List<PoiLocationEntity> searchLocationInfo(@QueryParam("q") String queryStr) {
        if (!StringUtils.isEmpty(queryStr)) {
            List<PoiLocationEntity> list = mapService.searchLocationInfo(queryStr);
            return list;
        } else {
            return null;
        }
    }

    @GET
    @Path("search/personnels")
    public List<PersonnelSearchDto> searchPersonnelInfo(@QueryParam("q") String queryStr,
                                                        @QueryParam("type") Integer prisonerType,
                                                        @QueryParam("locator") Integer locatorAddress) {
        List<PersonnelSearchDto> list = mapService.searchPersonnelInfo(queryStr, prisonerType, locatorAddress);
        return list;
    }

    @GET
    @Path("search/personnels/{x},{y}")
    public List<PersonnelSearchDto> searchPersonnelInfoByPos(@QueryParam("type") Integer prisonerType,
                                                             @PathParam("x") double x,
                                                             @PathParam("y") double y) {
        List<PersonnelSearchDto> list =
                mapService.searchPersonnelInfoInPolygon(prisonerType, GeomUtil.INSTANCE.createSmallPolygon(x, y));
        return list;
    }

    @GET
    @Path("search/assets")
    public List<AssetView> searchAssetInfo(@QueryParam("q") String queryStr,
                                           @QueryParam("locator") Integer locatorAddress) {
        return mapService.searchAssetInfo(queryStr, locatorAddress);
    }

    @GET
    @Path("search/assets/{x},{y}")
    public List<AssetView> searchAssetInfoByPos(@PathParam("x") double x,
                                                @PathParam("y") double y) {
        return mapService.searchAssetInfoInPolygon(GeomUtil.INSTANCE.createSmallPolygon(x, y));
    }

    @GET
    @Path("org/{orgId}/navigation")
    public PoiLocationEntity getOrgNavigation(@PathParam("orgId") Long orgId) {

        return mapService.getOrgNavigation(orgId);
    }

    //获取地图上的摄像头信息
    @GET
    @Path("cameras")
    public List<VideoCameraView> getVideoCameras(@QueryParam("floorNumber") Integer floorNumber) {
        UserPrincipal userPrincipal = (UserPrincipal) securityContext.getUserPrincipal();
        Integer permissionLevel = userPrincipal != null ? userPrincipal.getCameraPermissionLevel() : 3;

        SearchCriteria criteria = SearchCriteria.Builder.create();
        criteria.add(new Criterion("permissionLevel", Criterion.Operator.GTE, permissionLevel));
        if (floorNumber != null) {
            criteria.add(new Criterion("floorNumber", Criterion.Operator.EQ, floorNumber));
        }
        List<VideoCameraView> list = videoCameraService.getAllVideoCameraInfo(criteria, Sort.by(new ArrayList<>()));
        return list;
    }
}
